<?php

namespace App\Http\Validators\V1;

use App\Helpers\Utils;
use App\Http\Validators\AbstractValidator;

/**
 * @author zhaoqiying
 */
class TestPasswordValidator extends AbstractValidator
{

    /**
     * Validation rules
     *
     * @var Array
     */
    protected $rules = array(
        'phone'   => ['required','is_phone'],
        'code'        => ['required','integer'],
        'password' => ['required','alpha_num','size:32'],
    );

    /**
     * Validation messages
     *
     * @var Array
     */
    protected $messages = array(
        'phone.required' => '手机号必须输入',
        'phone.is_phone' => '请输入正确的手机号格式',
        'code.required' => '验证码必须传值',
        'code.integer' => '验证码必须是整数',
        'password.required' => '密码必须输入',
        'password.alpha_num' => '密码格式错误',
        'password.size' => '密码必须是MD5字符串',
    );

    /**
     * Validation codes
     *
     * @var Array
     */
    public $codes = array(
        'keywords.required' => '1001',
        'keywords.is_username' => '1002',
        'keywords.is_not_email' => '1003',
        'password.required' => '1004',
        'password.alpha_num' => '1005',
        'password.size' => '1006',
    );

    /*
     * 自定义验证规则或者扩展Validator类
     */

    public function before()
    {
        //自定义规则检查用户手机号
        $this->extend('is_phone',function($attribute,$value,$paramters){
            if (preg_match('/^1[3|4|5|7|8]\d{9}$/', $value)) {
                return true;
            } else {
                return false;
            }
        });
    }

}
